
package com.zymic.callrecord;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaRecorder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;

public class PhoneListener extends PhoneStateListener {
    File audioFile;

    MediaRecorder mediaRecorder; // = new MediaRecorder();

    AudioManager audioManager = null;

    Context c;

    //
    boolean iscall = false;

    //
    public PhoneListener(Context context) {
        c = context;
        iscall = false;
        audioManager = (AudioManager) c.getSystemService(Context.AUDIO_SERVICE);
    }

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        super.onCallStateChanged(state, incomingNumber);
        Toast.makeText(c, "手机状态变化，当前状态= " + state+" (接听=2;挂断=0)", Toast.LENGTH_SHORT).show();
        switch (state) {
            case TelephonyManager.CALL_STATE_OFFHOOK:

                try {

                    recordCallComment();

                } catch (IOException e) {
                    Log.i("test", " strart record error");
                    e.printStackTrace();
                    mediaRecorder.stop();
                    mediaRecorder.reset(); // You can reuse the object by going
                                           // back to setAudioSource() step
                    mediaRecorder.release();
                    mediaRecorder = null;
                }

                break;
            case TelephonyManager.CALL_STATE_IDLE:
                if(iscall)
                {
                stop();
                iscall=false;
                }

                break;
        }
    }

    public void stop() {
        if (mediaRecorder != null) {

            Toast.makeText(c, "开始保存录音文件", Toast.LENGTH_SHORT).show();
            try {
                mediaRecorder.stop();
                mediaRecorder.reset(); // You can reuse the object by going back
                                       // to setAudioSource() step
                mediaRecorder.release();
                mediaRecorder = null;
                Toast.makeText(c, "保存录音文件成功", Toast.LENGTH_SHORT).show();

            } catch (Exception e) {
                Toast.makeText(c, "不能保存录音文件", Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }
            
            removListener();
        }

       
    }

    private void removListener() {
        try {
            Toast.makeText(c, "开始移除监听", Toast.LENGTH_SHORT).show();
            TelephonyManager telephonymanager = (TelephonyManager) c
                    .getSystemService(Context.TELEPHONY_SERVICE);
            telephonymanager.listen(this, PhoneStateListener.LISTEN_NONE);
        } catch (Exception e) {
            Toast.makeText(c, "移除监听失败", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }

    //
    public void recordCallComment() throws IOException {
        Toast.makeText(c, "触发录音（电话下行）", Toast.LENGTH_SHORT).show();
        try {
            // audioManager.setSpeakerphoneOn(true);

            mediaRecorder = new MediaRecorder();
            // System.out.println(mediaRecorder);
            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_DOWNLINK);// MediaRecorder.AudioSource.MIC
            mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

            audioFile = File.createTempFile("record_scj", ".amr");
            mediaRecorder.setOutputFile(audioFile.getAbsolutePath());

            mediaRecorder.prepare();
            mediaRecorder.start();
            
            iscall=true;
            Toast.makeText(c, "录音程序已经启动，现在正在录音", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            Toast.makeText(c, "录音错误", Toast.LENGTH_SHORT).show();
            removListener();
            e.printStackTrace();
        }
       
    }

}
